#include "queue.h"
#include <stdio.h>

void initQueue(Queue* q) {
    q->front = -1;
    q->rear = -1;
}

int isEmpty(Queue* q) {
    return q->front == -1;
}

int isFull(Queue* q) {
    return (q->rear + 1) % MAX_SIZE == q->front;
}

void enqueue(Queue* q, int value) {
    if (isFull(q)) return;
    if (isEmpty(q)) q->front = 0;
    q->rear = (q->rear + 1) % MAX_SIZE;
    q->data[q->rear] = value;
}

int dequeue(Queue* q) {
    if (isEmpty(q)) return -1;
    int value = q->data[q->front];
    if (q->front == q->rear) {
        initQueue(q);
    }
    else {
        q->front = (q->front + 1) % MAX_SIZE;
    }
    return value;
}

void printQueue(Queue* q) {
    if (isEmpty(q)) return;
    for (int i = q->front; i != q->rear; i = (i + 1) % MAX_SIZE) {
        printf("%d ", q->data[i]);
    }
    printf("%d\n", q->data[q->rear]);
}

int searchQueue(Queue* q, int value) {
    if (isEmpty(q)) return -1;
    for (int i = q->front; i != q->rear; i = (i + 1) % MAX_SIZE) {
        if (q->data[i] == value) return i;
    }
    if (q->data[q->rear] == value) return q->rear;
    return -1;
}